<?php
/**
 * --------------------------------------------------------------
 * 公共函数库
 * Author: zd <kfzphp@126.com>
 * Date: 2018-06-01
 * Project: kfzphp.com
 * --------------------------------------------------------------
 */

/**
 * 判断图片与url
 * @param  string     $img [description]
 * @param  string     $url [description]
 * @return [type]          [description]
 */
function default_imgurl($img = '', $url = '')
{
    if(!empty($img) && file_exists('.' . $img)) {
        $arr['pic'] = !empty($img) ? $img : '';
        $arr['url'] = !empty($url) ? $url : 'javascript:;';
        return $arr;
    } else {
        return false;
    }
}

/**
 * 分解字符串成数组
 * 字符串格式：/upload/111.jpg|http://www.126.com,/upload/222.jpg|http://www.163.com'
 * @param  string     $string  [字符串]
 * @param  boolean    $foreach [是否循环成二维数组]
 * @return [type]              [description]
 */
function explode_string($string = '', $foreach = true)
{
    if($string) {
        $array = array();
        if($foreach) {
            $expl = explode(',', $string);
            foreach ($expl as $k => $v) {
                $e = explode('|', $v);
                $array[] = default_imgurl($e[0], $e[1]);
            }
        } else {
            $e = explode('|', $string);
            $array = default_imgurl($e[0], $e[1]);
        }
        return $array;
    }
    return '';
}